import birdFlyControl from "./birdFlyControl";

const {ccclass, property} = cc._decorator;

// 小鸟的集合控制
@ccclass
export default class birdManager extends cc.Component {

    // LIFE-CYCLE CALLBACKS:
    // 得分
    currenScore: number = 0;
    // 小鸟预设体
    @property(cc.Prefab)
    birdPrefab: cc.Prefab;
    // 小鸟产生间隔
    during: number = 2;

    // onLoad () {}

    start () {
        let scoreUI = this.node.parent.getChildByName('scoreShow').getChildByName('score');
        let gameover = this.node.parent.getChildByName('GameOver');
        
        // 使用计时器产生小鸟
        let sched = ()=> {
            let bird = cc.instantiate(this.birdPrefab);
            bird.setParent(this.node);
            bird.x = Math.random() * 220 - 110, bird.y = -80;

            // 让小鸟飞
            let script = bird.getComponent(birdFlyControl);
            script.fly();
            // 加分回调
            script.addScoreCallBack = ()=> {
                this.currenScore ++;
                console.log('当前得分：' +  this.currenScore);
                scoreUI.getComponent(cc.Label).string = String(this.currenScore);
            };
            // 游戏结束回调
            script.dieCallBack = () => {
                // 移除所有小鸟 停止所有动作
                this.node.removeAllChildren();
                this.node.stopAllActions();
                // 结束生成小鸟循环
                this.unschedule(sched);
                // 重新开始弹窗出现
                gameover.active = true;
            }
        }
        this.schedule(sched, this.during);
    }

    // update (dt) {}
}
